package repository

import (
	"gorm.io/gorm"
	"guliedu/model"
)

type IUCenterMemberRepository interface {
	// 注册
	Insert(member *model.UCenterMember) error
	// 根据用户名查询
	Select(username string) (*model.UCenterMember, error)
	// 发送邮箱验证码
	MemberSendEmail(to string)
	// 获取验证码
	GetMemberCode(email string) (string, error)
}

func NewIUCenterMemberRepository() IUCenterMemberRepository {
	return &UCenterMemberRepository{mysqlDb: db}
}

type UCenterMemberRepository struct {
	mysqlDb *gorm.DB
}

func (t *UCenterMemberRepository) Insert(member *model.UCenterMember) error {
	return t.mysqlDb.Create(&member).Error
}

func (t *UCenterMemberRepository) Select(username string) (member *model.UCenterMember, err error) {
	err = t.mysqlDb.Where("username = ?", username).Find(&member).Error
	return
}

func (t *UCenterMemberRepository) MemberSendEmail(to string) {
	sendEmail(to)
}

func (t *UCenterMemberRepository) GetMemberCode(email string) (string, error) {
	return client.Get("user:" + email).Result()
}
